/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.c0910g.entity;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author BachLe
 */
@Stateless
public class UserRadioCabFacade extends AbstractFacade<UserRadioCab> {
    @PersistenceContext(unitName = "RadioCabsProject-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserRadioCabFacade() {
        super(UserRadioCab.class);
    }

    //Admin use to login action
    public UserRadioCab login(String typename,String username){
        String eql ="SELECT u FROM UserRadioCab u WHERE u.userRole.roleName='" + typename + "' and u.uRCName = :uRCName";
        return (UserRadioCab)em.createQuery(eql).setParameter("uRCName", username).getSingleResult();
    }
    //Advertise use to login action
    public UserRadioCab loginAdmin(String username, String password){
        String eql ="SELECT u FROM UserRadioCab u WHERE u.uRCName = '" + username + "' and u.uRCPassword = '" + password + "'";
        return (UserRadioCab)em.createQuery(eql).getSingleResult();
    }
    //Driver, Registration use to Login action
    public UserRadioCab login(String typename,String username, String password){
        String eql ="SELECT u FROM UserRadioCab u WHERE u.userRole.roleName='" + typename + "' and u.uRCName = '" + username + "' and u.uRCPassword = '" + password + "'";
        return (UserRadioCab)em.createQuery(eql).getSingleResult();
    }
    public UserRadioCab getUserRadioCabByID(String userid){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.urcid = :urcid";
        return (UserRadioCab)em.createQuery(eql).setParameter("urcid", userid).getSingleResult();
    }
    public List<UserRadioCab> getAllUserRadioCabs(){
        String eql = "SELECT u FROM UserRadioCab u where u.uRCName <> 'Admin'";//lay het tru ADMIN
        return em.createQuery(eql).getResultList();
    }

    public List<UserRadioCab> getAllUserByCity(String city){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.uRCCity = :uRCCity";
        return em.createQuery(eql).setParameter("uRCCity", city).getResultList();
    }
    public int updateStatusUser(Integer userid){
        String eql = "UPDATE UserRadioCab u SET u.uRCStatus = false where u.urcid = :userid";
        return em.createQuery(eql).setParameter("userid", userid).executeUpdate();
    }

    public int updateStatusPayUser(Integer userid){
        String eql = "UPDATE UserRadioCab u SET u.uRCStatus = true WHERE u.urcid = :userid";
        return em.createQuery(eql).setParameter("userid", userid).executeUpdate();
    }
    public UserRadioCab loginForAll(String rolename, String username, String password){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName "
                + "and u.uRCName = :uRCName and u.uRCPassword = :uRCPassword";
        return (UserRadioCab)em.createQuery(eql).setParameter("roleName", rolename)
                .setParameter("uRCName", username)
                .setParameter("uRCPassword", password).getSingleResult();
    }
    
    public List<UserRadioCab> getListingByCity(String city){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName and u.uRCCity = :uRCCity";
        return em.createQuery(eql).setParameter("roleName", "Listing").setParameter("uRCCity", city).getResultList();
    }

    public List<UserRadioCab> getDriverByCity(String city){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName and u.uRCCity = :uRCCity";
        return em.createQuery(eql).setParameter("roleName", "Driver").setParameter("uRCCity", city).getResultList();
    }

    public List<UserRadioCab> getAdvertiseByCity(String city){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName and u.uRCCity = :uRCCity";
        return em.createQuery(eql).setParameter("roleName", "Advertise").setParameter("uRCCity", city).getResultList();
    }

    public List<UserRadioCab> getListing(){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Listing").getResultList();
    }

    public List<UserRadioCab> getDriver(){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Driver").getResultList();
    }

    public List<UserRadioCab> getAdvertise(){
        String eql = "SELECT u FROM UserRadioCab u WHERE u.userRole.roleName = :roleName";
        return em.createQuery(eql).setParameter("roleName", "Advertise").getResultList();
    }

}
